﻿using System;
using System.Xml;


namespace PFD
{
    public class Vector<T> : Object
    {
        public T[] elements;

        public Vector(int nDim)
        {
            if (nDim < 1)
                throw new ArgumentOutOfRangeException("nDim", "Argument is less than one!");

            elements = new T[nDim];
        }

        public Vector(T[] e)
        {
            elements = new T[e.Length];

            for (int i = 0; i < e.Length; i++)
                elements[i] = e[i];
        }

        static public void ReadVector2D(XmlNode vectorref, ref GeometryArray vertices)
        {
            int nCurrentElement = 0;
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("U"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("V"));

        }

        static public void ReadVector3D(XmlNode vectorref, ref GeometryArray vertices)
        {
            int nCurrentElement = 0;
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("X"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Y"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Z"));
        }

        static public void ReadVector4D(XmlNode vectorref, ref GeometryArray vertices)
        {
            int nCurrentElement = 0;
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("X"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Y"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Z"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("A"));
        }

        static public void ReadQuaternion(XmlNode vectorref, ref GeometryArray vertices)
        {
            int nCurrentElement = 0;
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("A"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("X"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Y"));
            SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Z"));
        }

        static public void ReadVectors(XmlNode array, ref GeometryArray vertices, int vectordimension)
        {
            int nCurrentElement = 0;
            foreach (XmlNode vectorref in array.ChildNodes)
            {
                switch (vectorref.Name)
                {
                    case "VECTOR3D":
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("X"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Y"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Z"));
                        break;

                    case "VECTOR2D":
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("U"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("V"));
                        break;

                    case "VECTOR4D":    //  difference between VECTOR4D and a quaternion is that the A is an alpha value and is located at the end of the strucure
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("X"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Y"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Z"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("A"));
                        break;

                    case "QUATERNION":  //  difference between QUATERNION and VECTOR4D is that the first element is an angle here
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("A"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("X"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Y"));
                        SystemVariables.ReadParamValue(ref vertices, nCurrentElement++, vectorref.SelectSingleNode("Z"));
                        break;
                }
            }
        }

    }

}
